package com.homelock.thread;

import com.homelock.common.enums.TriggerTypeEnum;
import com.homelock.common.enums.sms.SmsSendTypeEnum;
import com.homelock.entity.HlDevInfo;
import com.homelock.entity.HlDevUser;
import com.homelock.entity.HlFamilyLinkage;
import com.homelock.service.SmsSendApiService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * 异步发送报警信息任务
 */
@Component
public class AlarmSmsAsyncTask {
    protected final Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private SmsSendApiService smsSendApiService;
    /**
     * 异步发送报警短信
     * @param triggerTypeEnum
     * @param hlDevInfo
     * @param openUserName
     * @throws
     */
    @Async("asyncTaskExecute")
    public void runAlarmSmsAsyncTask(TriggerTypeEnum triggerTypeEnum,HlDevInfo hlDevInfo,String openUserName)  {
        logger.info("进入【runAlarmSmsAsyncTask】的线程:【{}】执行中，开始进行发送报警短信操作",Thread.currentThread().getName());
        logger.info("开始发送{}短信",triggerTypeEnum.getName());
       if(triggerTypeEnum.getCode() == 21){
           smsSendApiService.sendAlarmSms(hlDevInfo, triggerTypeEnum,openUserName);
       }else if (triggerTypeEnum.getCode() == 28){
           smsSendApiService.sendAlarmSms(hlDevInfo, triggerTypeEnum,openUserName);
       }else{
           smsSendApiService.sendAlarmSms(hlDevInfo, triggerTypeEnum,openUserName);
       }
    }

    /**
     * 异步发送报警短信
     * @param smsSendType  短信类型
     * @param hlDevInfo
     * @param openUserName
     * @throws
     */
    @Async("asyncTaskExecute")
    public void runSmsSendByTypeTaskAsync(SmsSendTypeEnum smsSendType,HlDevInfo hlDevInfo,String openUserName)  {
        logger.info("[zx_sms_task_begin] sendType: {}, devId: {}, userName: {}", smsSendType, hlDevInfo.getId(), openUserName);
        smsSendApiService.sendSmsBySmsSendType(hlDevInfo, smsSendType,openUserName);
    }

    /**
     * 异步发送报警短信
     *
     * @param smsSendTypeEnum 短信类型
     * @param hlDevInfo 设备信息
     * @param openUserName 开门人
     * @throws
     */
    @Async("asyncTaskExecute")
    public void runSmsAsyncTask(HlDevInfo hlDevInfo, SmsSendTypeEnum smsSendTypeEnum, String openUserName) {
        logger.info("进入【runAlarmSmsAsyncTask】的线程:【{}】执行中，开始进行发送报警短信操作", Thread.currentThread().getName());
        logger.info("devId： {}, 开始发送 {} 短信", hlDevInfo.getId(), smsSendTypeEnum);
        smsSendApiService.sendSmsBySmsSendType(hlDevInfo, smsSendTypeEnum, openUserName);
    }


    /**
     * 异步发送亲情短信
     * @param hlDevInfo 设备信息
     * @param sender 开门用户
     * @param familyLinkages 亲情关联用户
     */
    @Async
    public void runFamilySmsAsyncTask(HlDevInfo hlDevInfo, HlDevUser sender, List<HlFamilyLinkage> familyLinkages)  {
        logger.info("进入【runFamilySmsAsyncTask】的线程:【{}】执行中，开始进行发送亲情短信操作",Thread.currentThread().getName());
        logger.info("开始发送{}开门的亲情短信",sender.getDevUserName());
        smsSendApiService.sendFamilySms(hlDevInfo,sender,familyLinkages);
    }

}
